//请求路径常量
import * as req_con from '../../utils/constant'
//数据
import {roomTypeList} from '../sql_data/data'
export let roomTypeDao = (url,params) => {
    //模仿数据库CRUD操作
    switch (url) {
         //RoomType
        case req_con.ROOMTYPE_CONSTANT.ROOMTYPELISTGET:
            return  findRoomTypeList(params)
        case req_con.ROOMTYPE_CONSTANT.ROOMTYPEADD:
            return  roomTypeAdd(params)
        case req_con.ROOMTYPE_CONSTANT.ROOMTYPEELETE:
            return  roomTypeDelete(params)
        case req_con.ROOMTYPE_CONSTANT.ROOMTYPEUPDATE:
            return  roomTypeUpdate(params)
        default:
            break;
    }
}

let findRoomTypeList = () => {
    return roomTypeList
}


let roomTypeAdd = (params) => {
    console.log(params);
    let index = roomTypeList.length+1
    params.id=index 
    roomTypeList.push(params)
    console.log(roomTypeList);
    return {
        success: true,
        message: '添加成功'
    }
}


let roomTypeDelete = (params) => {
    console.log(params);
    let index = roomTypeList.findIndex(v=>v.id===params.id)
    roomTypeList.splice(index,1)
    //重新设置id
    roomTypeList.forEach((v,index)=>{
        v.id=index+1
    })
    return {
        success: true,
        message: '删除成功'
    }
}


let roomTypeUpdate = (params) => {
    let newRoomTypeList = []
    roomTypeList.forEach((v)=>{
        newRoomTypeList.push(JSON.parse(JSON.stringify(v))) 
    })
    newRoomTypeList.forEach((v,index)=>{
        if(v.id===params.id){
            newRoomTypeList[index] = params 
        }
    })
    return {
        success: true,
        message: '修改成功',
        newRoomTypeList
    }
}